package com.mcppro.aidatabase.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * 开发环境下的宽泛CORS配置
 * 用于解决开发阶段的跨域问题
 * 
 * @author MCP Pro
 */
@Configuration
@Profile({"dev", "default"}) // 仅在开发环境启用
public class DevCorsConfig {

    @Bean("devCorsConfigurationSource")
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        
        // 允许所有本地开发地址
        configuration.setAllowedOriginPatterns(Arrays.asList(
            "http://localhost",
            "http://localhost:*",
            "http://127.0.0.1", 
            "http://127.0.0.1:*",
            "http://0.0.0.0:*"
        ));
        
        // 允许所有HTTP方法
        configuration.setAllowedMethods(Arrays.asList("*"));
        
        // 允许所有请求头
        configuration.setAllowedHeaders(Arrays.asList("*"));
        
        // 允许凭据
        configuration.setAllowCredentials(true);
        
        // 预检请求缓存时间
        configuration.setMaxAge(3600L);
        
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        
        return source;
    }
}